﻿using GBT.ParamEntity;
using GBT.SystemBusinessCore;
using GBT.SystemEnties;
using SqlSugar;
using System.Linq;

namespace QueryDataLogic.Core
{
    public class AnnounceManage
    {
        BasicInfo bi = new BasicInfo();
        DbContext<Announcement> dbContext = null;

        public object GetAnnounce(System_Announcement _Announcement, ref int totalNumber, ref int totalPage)
        {
            var dbName = bi.GetDBName((int)_Announcement.DistrictID);
            dbContext = new DbContext<Announcement>(dbName);
            Manage_District district = new Manage_District();
            district.DistrictID = (int)_Announcement.DistrictID;
            district = bi.GetDistrictInfo(district);
            var db = dbContext.Db;
            var announcementList = db.Queryable<Announcement, MealUser>((a, mu) => new object[] {
                JoinType.Left, a.UserID == mu.UserID,
            })
            .Where((a, mu) => a.DistrictID == _Announcement.DistrictID)
            .WhereIF(_Announcement.ID != null, (a, mu) => a.ID == _Announcement.ID)
            .WhereIF(_Announcement.UserID != null, (a, mu) => a.UserID == _Announcement.UserID)
            .WhereIF(!string.IsNullOrEmpty(_Announcement.Title), (a, mu) => a.Title.Contains(_Announcement.Title))
            .WhereIF(!string.IsNullOrEmpty(_Announcement.Text), (a, mu) => a.Text.Contains(_Announcement.Text))
            .WhereIF(_Announcement.IsCarousel != null, (a, mu) => a.IsCarousel == _Announcement.IsCarousel)
            .WhereIF(_Announcement.IsDelete != null, (a, mu) => a.IsDelete == _Announcement.IsDelete)
            .Select((a, mu) => new
            {
                a.ID,
                a.UserID,
                a.DistrictID,
                a.Title,
                a.Text,
                a.IsCarousel,
                a.AntDate,
                a.IsDelete,
                mu.Number,
                mu.Name,
                a.CreationDate,
            }).ToPageList(_Announcement.PageIndex, _Announcement.PageSize, ref totalNumber, ref totalPage);

            var announcementListNew = announcementList.Select(t => new
            {
                t.ID,
                t.UserID,
                t.DistrictID,
                t.Title,
                t.Text,
                t.IsCarousel,
                t.IsDelete,
                Carousel = t.IsCarousel == false || t.IsCarousel == null ? "禁用" : "启用",
                t.AntDate,
                t.Number,
                t.Name,
                t.CreationDate,
                district.DistrictNumber,
                district.DistrictName,
            }).ToList();

            return announcementListNew;
        }
    }
}
